<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="__STATIC__/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="__STATIC__/css/font.css">
    <link rel="stylesheet" href="__STATIC__/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="__STATIC__/js/xadmin.js"></script>
    <script type="text/javascript" src="__STATIC__/lib/layui/layui.js" charset="utf-8"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<style>
    /*.layui-table-body .layui-table-cell{*/
    /*height:60px;*/
    /*line-height: 60px;*/
    /*}*/
</style>

<body>
<div class="layui-fluid">
    <!--<div class="demoTable">
        搜索ID：
        <div class="layui-inline">
            <input class="layui-input" name="id" id="demoReload" autocomplete="off">
        </div>
        <button class="layui-btn" data-type="reload">搜索</button>
    </div>-->
    <table class="layui-hide" id="test" lay-filter="test"></table>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>
</div>
<script>
    layui.use(['table','form'], function(){
        var table = layui.table
            ,form = layui.form;
        table.render({
            elem: '#test'
            ,url:'{:url("yuyue/yuyueListData")}'
            ,title: '权限表'
            ,toolbar: 'default'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'ID', width:80, fixed: 'left',sort:true,unresize: true}
                ,{field:'jzname', title:'驾照'}
                ,{field:'km', title:'科目'}
                ,{field:'typeid', title:'类型',templet:function(d){
                    if(d.typeid==0){
                        return '周一到周六';
                    }else{
                        return '周天';
                    }
                }}
                ,{field:'begin_end', title:'日期范围'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]]
            ,id: 'testReload'
            ,page: true
            ,done:function () {

            }
        });

        var $ = layui.$, active = {
            reload: function(){
                var demoReload = $('#demoReload');

                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        key: {
                            id: demoReload.val()
                        }
                    }
                });
            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'add':
                    window.location.href='/index.php/admin/yuyue/yuyueAdd';
                    break;
                case 'update':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else if(data.length > 1){
                        layer.msg('只能同时编辑一个');
                    } else {
                        window.location.href='/index.php/admin/yuyue/yuyueEdit?id='+checkStatus.data[0].id;
                    }
                    break;
                case 'delete':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else {
                        var ids=[];
                        $.each(data,function(k,v){
                            ids.push(v.id);
                        });
                        layer.confirm('真的删除行么', function() {
                            $.post({
                                url: '{:url("yuyue/del")}',
                                data: {ids: ids},
                                dataType: 'json',
                                success: function (re) {
                                    if (re.code == 1) {
                                        layer.msg('删除成功', {
                                            icon: 1,
                                            time: 2000,
                                            end: function () {
                                                location.reload();
                                            }
                                        });
                                    }
                                }
                            })
                        });
                    break;
                };
            }
        });

        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'edit'){
                window.location.href='/index.php/admin/yuyue/yuyueEdit?id='+data.id;
            } else if(layEvent === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.post({
                        url:'/index.php/admin/yuyue/del',
                        data:{'ids':data.id},
                        success:function(re){
                            if(re.code==1){
                                layer.msg('删除成功',
                                    {
                                        icon:1,
                                        time:2000,
                                        end:function(){
                                            location.reload();
                                        }
                                    }
                                )
                            }else{
                                layer.msg('删除失败',{icon:1,time:2000});
                            }
                        }
                    })
                });
            }
        });

    });
</script>
</body>

</html>